#include "qelib.h"



static qe_const_str usages[] = {
    "memtest [options] [[--] args]",
    "memtest [options]",
    QE_NULL,
};

static qe_u8 mem_zone[4096];
static void cmd_mem_zone(void)
{
	qe_printf("%p\r\n", mem_zone);
}
QE_SHELL_EXPORT_CMD_EXEC(memzone, cmd_mem_zone, memzone commands);

static void cmd_memtest(int argc, char *argv[])
{
    int i;
    qe_ret ret;
    qe_uint start = 0;
    qe_uint size = 0;
    qe_uint mode = 0;
    qe_u8 *buf;
    qe_u8 data = 0;
    qe_argparse parser;

    qe_option options[] = {
        QE_OPT_INTEGER(QE_NULL, "start", "memtest start", &start),
        QE_OPT_INTEGER(QE_NULL, "size", "memtest size", &size),
        QE_OPT_INTEGER('m', "mode", "memtest mode 0:zero 1:one 2:0xAA 3:0x55 4:0xFF", &mode),
        QE_OPT_HELP('h', "--help", "mmetest help"),
        QE_OPT_END(),
    };

    qe_argp_init(&parser, options, usages,
    	"\nmemtest command.",
		"\nmemtest command.");

    ret = qe_argp_parse(&parser, argc, (qe_const_str *)argv);
    if (ret != qe_ok) {
        return;
    }

    switch (mode) {

    case 0:
        data = 0;
        break;

    case 1:
        data = 1;
        break;

    case 2:
        data = 0xAA;
        break;

    case 3:
        data = 0x55;
        break;

    case 4:
        data = 0xFF;
        break;

    default:
        qe_printf("Invalid mode %d\r\n", mode);
        return;
    }

    qe_printf("memtest start at %x %d\r\n", start, size);

    buf = (qe_u8 *)start;
    for (i=0; i<size; i++) {
        buf[i] = data;
    }

    qe_printf("write finish\r\n");

    for (i=0; i<size; i++) {
        if (buf[i] != data) {
            qe_printf("memtest error at %d\r\n", i);
            return;
        }
    }

    qe_printf("memtest success at %x %d\r\n", start, size);
}

QE_SHELL_EXPORT_CMD_EXEC(memtest, cmd_memtest, memtest commands);
